package com.power.structure.stack;

/**
 * @description:
 * @author: jiangnan
 * @time: 2021/6/11 9:52 下午
 */
public class SeqStack {
    int[] data;
    private final int CAPACITY = 10;
    private int size;

    public SeqStack() {
        data = new int[CAPACITY];
    }
    public int size() {
        return size;
    }

    public void push(int element) {
        if (isFull()) {
            throw new ArrayIndexOutOfBoundsException("栈满啦");
        }
        data[size++] = element;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    private boolean isFull() {
        return size == CAPACITY;
    }

    public int pop() {
        if (isEmpty()) {
            throw new IllegalStateException("栈空了,不能取出数据了");
        }
        int index = size - 1;
        size--;
        return data[index];
    }

    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.print(" " + data[i]);
        }
        System.out.println("\n");
    }


    public static void main(String[] args) {
        SeqStack stack = new SeqStack();
        for (int i = 0; i < 10; i++) {
            stack.push(i);
        }
        stack.display();
        System.out.println("stack pushed size:: " + stack.size());
        System.out.println("pop()::");
        for (int i = 0; i < 10; i++) {
            System.out.print(stack.pop() + " ");
        }
        System.out.println("\n");
        stack.push(11);
        stack.push(12);
        stack.display();
        System.out.println("stack poped size:: " + stack.size());
    }
}
